///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.logger {
	import mx.logging.LogEvent;
	import mx.logging.targets.LineFormattedTarget;
	import mx.formatters.DateFormatter;
	
	/**
	 * Target for the Flex Logging framework.
	 */
	public class TraceTarget extends LineFormattedTarget {
		private var _timeFormatter:DateFormatter;
		
		public function TraceTarget() {
			super();
			_timeFormatter = new DateFormatter();
			_timeFormatter.formatString = "L:NN:SS:QQQ";
		}
		
		public override function logEvent(evt:LogEvent):void {
			trace(constructMessage(evt));
		}
		
		private function constructMessage(evt:LogEvent):String {
 			var time:String= this.includeTime?_timeFormatter.format(new Date())+" ":"";
 			return time+"["+getLogLevel(evt.level)+"] "+evt.currentTarget.category+": "+evt.message;
 		}
 		
 		private function getLogLevel(level:int):String {
 			var str:String;
 			switch (level) {
 				case 0:
 					str = "ALL";
 					break;
 				case 2:
 					str = "DEBUG";
 					break;
 				case 4:
 					str = "INFO";
 					break;
 				case 6:
 					str = "WARN";
 					break;
 				case 8:
 					str = "ERROR";
 					break;
 				case 1000:
 					str = "FATAL";
 					break;
 			}
 			return str;
 		}
	}
}